$(function() {
	$(".logo").ifixpng();
	if ($.validator) {
		$.validator
				.addMethod(
						"phone",
						function(value, element) {
							return this.optional(element)
									|| /((1(([35][0-9])|(47)|[8][01236789]))\d{8})|(0\d{2,3}(\-)?\d{7,8})$/
											.test(value);
						}, "请输入正确的电话号码!");
		$.validator.addClassRules({
			phone : {
				phone : true
			}
		});
	}
});
var more = {
	preUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input[name='more.previewImage']").val(data.url);
			var fileQue = $wrapper.siblings("div.thumbImg");
			fileQue.empty();
			var img = $("<img src='" + resourceRootUrl + data.url
					+ "' height='150px'/>");
			fileQue.append(img);
		});
	},
	backImageUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input[name='more.backImage']").val(data.url);
			var fileQue = $wrapper.siblings("div.thumbImg");
			fileQue.empty();
			var img = $("<img src='" + resourceRootUrl + data.url
					+ "' height='150px'/>");
			fileQue.append(img);
		});
	},
	detailUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input#imgUrl").val(data.url);
			var fileQue = $wrapper.siblings("div.thumbImg");
			fileQue.empty();
			var img = $("<img src='" + resourceRootUrl + data.url
					+ "' height='150px'/>");
			fileQue.append(img);
		});
	}
}
var news = {
	preUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input[name='news.previewImage']").val(data.url);
			var fileQue = $wrapper.siblings("div.thumbImg");
			fileQue.empty();
			var img = $("<img src='" + resourceRootUrl + data.url
					+ "' height='150px'/>");
			fileQue.append(img);
		});
	},
	detailUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input#imgUrl").val(data.url);
			var fileQue = $wrapper.siblings("div.thumbImg");
			fileQue.empty();
			var img = $("<img src='" + resourceRootUrl + data.url
					+ "' height='150px'/>");
			fileQue.append(img);
		});
	},
	videoUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var $wrapper = $this[i].wrapper;
			$wrapper.siblings("input#videoUrl").val(data.videoUrl);
		});
	},
	picsUploadSuccess : function(file, data, response) {
		if (!response) {
			return;
		}
		var $this = $(this);
		data = eval("(" + data + ")");
		$this.each(function(i) {
			var count = $(".pics .column").length;
			var url = resourceRootUrl + data.url;
			$("#columnTmpl").tmpl([ {
				COUNT : count,
				URL : url,
				IMAGE_URL : data.url
			} ]).appendTo(".pics");

			$(".portlet-header .ui-icon").click(function() {
				$(this).parents(".column").remove();
				$(".column").find("input[name$=orders]").each(function(i) {
					$(this).val(i);
				});
			});
		});
	}
};
/**
 * 只能输入数字，如果为空，返回0
 */
function onlyNum(obj) {
	var str = obj.value;
	str = str.replace(/[^\d]/g, '');
	if (str == "")
		str = 0;
	if (str.length > 1 && str.charAt(0) == '0')
		str = str.substr(1);
	obj.value = str;
}

Array.prototype.max = function() { // 最大值
	return Math.max.apply({}, this);
};